<?php
global $_W,$_GPC;
$op = !empty($_GPC['op'])?$_GPC['op']:'display';

if($op=='display'){
	$pindex = max(1, intval($_GPC['page']));
	$psize = 10;
	$content ='';
	$type = intval($_GPC['type']);
	$keyword = trim($_GPC['keyword']);
	if (!empty($keyword)) {
		switch($type) {
			case 2 :
				$content .= " AND mobile LIKE '%{$keyword}%' ";
				break;
			case 3 :
				$content .= " AND nickname LIKE '%{$keyword}%' ";
				break;
			default :
				$content .= " AND realname LIKE '%{$keyword}%' ";
		}
	}
	$members = pdo_fetchall("select * from".tablename("weliam_indiana_member")."where uniacid = {$_W['uniacid']} {$content} LIMIT " . ($pindex - 1) * $psize . ',' . $psize);	
	foreach ($members as $key => $value) {
		load()->model('mc');
		$uid = mc_openid2uid($value['openid']);
		$member_info = mc_fetch($uid, array('credit1','credit2'));
		$members[$key]['credit1'] = $member_info['credit1'];
		$members[$key]['credit2'] = $member_info['credit2'];
		$members[$key]['uid'] = $uid;
	}
	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('weliam_indiana_member') . " WHERE uniacid = {$_W['uniacid']} {$content} ");
	$pager = pagination($total, $pindex, $psize);
	include $this->template('member');
}

if($op=='buyre'){
	$openid = $_GPC['openid'];
	$uniacid = $_W['uniacid'];
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
	$condition = '';
	if (empty($starttime) || empty($endtime)) {
		$starttime = strtotime(date('Y-m-d'));
		$endtime = strtotime(date('Y-m-d',strtotime('+1 day')));
	}
	if (!empty($_GPC['time'])) {
		$starttime = strtotime($_GPC['time']['start']);
		$endtime = strtotime($_GPC['time']['end']) ;
		$condition .= " AND  createtime >= '{$starttime}' AND  createtime <= '{$endtime}' ";
	}
	
	$goodses = pdo_fetchall("SELECT * FROM ".tablename('weliam_indiana_consumerecord')." WHERE uniacid = '{$uniacid}' and status = 1 and openid = '{$openid}' ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize);
	foreach($goodses as$key=>$value){
		$period = pdo_fetch("SELECT goodsid,periods FROM " . tablename('weliam_indiana_period') . " where period_number = '{$value['period_number']}'");
		$goods = pdo_fetch("SELECT title,picarr FROM " . tablename('weliam_indiana_goodslist') . " where id = '{$period['goodsid']}'");
		$info = m('member')->getInfoByOpenid($value['openid']);
		$goodses[$key]['title'] = $goods['title'];
		$goodses[$key]['picarr'] = $goods['picarr'];
		$goodses[$key]['periods'] = $period['periods'];
		$goodses[$key]['avatar'] = $info['avatar'];
		$goodses[$key]['nickname'] = $info['nickname'];
	}

	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('weliam_indiana_consumerecord') . " WHERE uniacid = '{$uniacid}' and status = 1 and openid = '{$openid}' ");
	$pager = pagination($total, $pindex, $psize);

	include $this->template('records');
}

if($op=='recharge'){
	$openid = $_GPC['openid'];
	$uniacid = $_W['uniacid'];
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
	$condition = '';
	if (empty($starttime) || empty($endtime)) {
		$starttime = strtotime(date('Y-m-d'));
		$endtime = strtotime(date('Y-m-d',strtotime('+1 day')));
	}
	if (!empty($_GPC['time'])) {
		$starttime = strtotime($_GPC['time']['start']);
		$endtime = strtotime($_GPC['time']['end']) ;
		$condition .= " AND  createtime >= '{$starttime}' AND  createtime <= '{$endtime}' ";
	}
	
	$goodses = pdo_fetchall("select * from".tablename('weliam_indiana_rechargerecord')."where openid = '{$openid}' and status = 1 and uniacid = '{$_W['uniacid']}' $condition order by id desc limit ".($pindex - 1) * $psize.','.$psize);
	foreach($goodses as$key=>$value){
		$info = m('member')->getInfoByOpenid($value['openid']);
		$goodses[$key]['avatar'] = $info['avatar'];
		$goodses[$key]['nickname'] = $info['nickname'];
	}

	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('weliam_indiana_rechargerecord') . " WHERE uniacid = '{$uniacid}' and status = 1 and openid = '{$openid}' $condition ");
	$pager = pagination($total, $pindex, $psize);

	include $this->template('recharge');
}